﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

using SPaDevToolkit.Administration;
using SPaDevToolkit.Diagnostics;

using Northwind.Common.Diagnostics;

namespace Northwind.ServiceObjects
{
    [Guid("00D6487F-5247-4340-9E6B-E7740A6AD922")]
    [SupportedServiceApplication(NorthwindApplication.SupportedApplicationClassId, "1.0.0.0", typeof(NorthwindServiceProxy))]
    public sealed class NorthwindApplicationProxy : SPIisWebServiceApplicationProxy // SPaCslaWebServiceApplicationProxy
    {
        [Persisted]
        SPServiceLoadBalancer _loadBalancer;

        #region Constructors
        public NorthwindApplicationProxy()
        {
        }

        public NorthwindApplicationProxy(string name, NorthwindServiceProxy serviceProxy, Uri serviceEndpointUri)
            : base(name, serviceProxy, serviceEndpointUri)
        {
            _loadBalancer = new SPRoundRobinServiceLoadBalancer(serviceEndpointUri);
        }
        #endregion

        #region Overrides
        public override string TypeName
        {
            get { return "Northwind Application Proxy"; }
        }

        //protected override string ClientConfigurationPath
        //{
        //    get { return @"WebClients\Northwind"; }
        //}

        public override void Provision()
        {
            Logger.WriteTrace(string.Format("Provisioning service application operation proxy '{0}'", Name), 0, TraceSeverity.High, AreaNames.Northwind, CategoryNames.Service);
            try
            {
                _loadBalancer.Provision();
                base.Provision();
                Update();
                Logger.WriteTrace(string.Format("Finished Provisioning service application operation proxy '{0}'", Name), 0, TraceSeverity.High, AreaNames.Northwind, CategoryNames.Service);
            }
            catch (Exception ex)
            {
                Logger.WriteException(ex, string.Format("Error provisioning service application proxy '{0}'", Name), 0, EventSeverity.Error, AreaNames.Northwind, CategoryNames.Service);
                throw;
            }
        }

        public override void Unprovision(bool deleteData)
        {
            Logger.WriteTrace(string.Format("Unprovisioning service application operation proxy '{0}'", Name), 0, TraceSeverity.High, AreaNames.Northwind, CategoryNames.Service);
            try
            {
                _loadBalancer.Unprovision();
                base.Unprovision(deleteData);
                Update();
                Logger.WriteTrace(string.Format("Finished Unprovisioning service application operation proxy '{0}'", Name), 0, TraceSeverity.High, AreaNames.Northwind, CategoryNames.Service);
            }
            catch (Exception ex)
            {
                Logger.WriteException(ex, string.Format("Error Unprovisioning service application proxy '{0}'", Name), 0, EventSeverity.Error, AreaNames.Northwind, CategoryNames.Service);
                throw;
            }
        }
        #endregion
    }

    #region NorthwindApplicationProxyCollection
    [Guid("0AF7A388-C497-4413-9C8E-A51AF341E734")]
    internal sealed class NorthwindApplicationProxyCollection : SPPersistedChildCollection<NorthwindApplicationProxy>
    {
        // Methods
        internal NorthwindApplicationProxyCollection(NorthwindServiceProxy serviceProxy)
            : base(serviceProxy)
        {
        }
    }
    #endregion

}
